{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5aa03ca1d7b9479595f7b552a36fbdff",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"A beam deforming under its own weight.\"\"\"\n",
    "from dolfin import *\n",
    "\n",
    "# Scaled variables\n",
    "l, w = 1, 0.1\n",
    "mu_, lambda_ = 1, 1\n",
    "rho = 10\n",
    "gamma = (w/l)**2\n",
    "wind = (0, 0.0, 0)\n",
    "\n",
    "# Create mesh and define function space\n",
    "mesh = BoxMesh(Point(0, 0, 0), Point(l, w, w), 50, 5, 5)\n",
    "V = VectorFunctionSpace(mesh, \"P\", 1)\n",
    "\n",
    "# Define boundary condition\n",
    "def clamped_boundary(x, on_boundary):\n",
    "    return on_boundary and (near(x[0], 0) or near(x[0], l))\n",
    "bc = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary)\n",
    "\n",
    "# Define strain and stress\n",
    "def epsilon(u):\n",
    "    return 0.5 * (nabla_grad(u) + nabla_grad(u).T)\n",
    "\n",
    "def sigma(u):\n",
    "    return lambda_ * nabla_grad(u) * Identity(3) + 2 * mu_ * epsilon(u)\n",
    "\n",
    "# Define variational problem\n",
    "u = TrialFunction(V)\n",
    "v = TestFunction(V)\n",
    "f = Constant((0, 0, -rho * gamma))\n",
    "T = Constant(wind)\n",
    "a = inner(sigma(u), epsilon(v)) * dx\n",
    "L = dot(f, v) * dx + dot(T, v) * ds\n",
    "\n",
    "# Compute solution\n",
    "u = Function(V)\n",
    "solve(a == L, u, bc)\n",
    "\n",
    "\n",
    "################################ Plot solution\n",
    "from vtkplotter.dolfin import *\n",
    "\n",
    "plot(u, mode=\"displaced mesh\", shading='flat')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
